import { Component, OnInit } from '@angular/core';
import { StorageService } from 'src/app/services/storage.service';

@Component({
  selector: 'app-todo-list',
  templateUrl: './todo-list.component.html',
  styleUrls: ['./todo-list.component.css']
})
export class TodoListComponent implements OnInit {

  constructor(public storage:StorageService) {
    // let s = storage.get()
    // console.log(s)
  }

  ngOnInit(): void {
    console.log('页面刷新会出发这个声明周期函数')
    var todolist = this.storage.get('todolist')
    if(todolist){
      this.todoList = todolist;
    }
  }
  public task:string='';
  public completeList:any=[];
  public doingList:any=[];
  public todoList:any=[];
  doAdd(e:any){
    if(e.keyCode=='13'){
      if(!this.todolistHasTask(this.todoList,this.task)){
        this.todoList.push({title:this.task,status:0}) ;
        this.task='';
        this.storage.set('todolist',this.todoList); //用到this一定要注意this指向
        //forEach和定时器中this指向window，若要指向当前示例，一种用箭头函数，一种用？？？改变一下      
      }
      else{
        alert("数据已存在");
        this.task='';
      }
      
    }

  }
  doDelete(key:any){
    console.log(key)
    this.todoList.splice(key,1);
    this.storage.set('todolist',this.todoList); //用到this一定要注意this指向

  }
  todolistHasTask(todolist:any,task:any){
    let bl = false;
    todolist.forEach((element:any) => {
      if(element.title == task) bl = true;
    });
    return bl;
  }

  changeCheck(){
    console.log('111')
    this.storage.set('todolist',this.todoList); //用到this一定要注意this指向
  }

}
